/*
 * $Id: EncryptUtil.java,v 1.2 2010/05/09 11:09:29 ync Exp $
 * 
 * 文件名称：EncryptUtil.java
 * 
 * 创建日期：Apr 4, 2007
 * 
 * 版权所有：www.gzld.net科技有限公司
 */
/**
 * 
 */
package cn.org.timespace.easyframe.util;

import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;


public class EncryptUtil {
	
	public static String getEncodedPassword(String clearTextPassword) {
        MessageDigest md = null;
        clearTextPassword = clearTextPassword.trim();
        
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(clearTextPassword.getBytes());
        } catch (Exception e) {
            throw new IllegalArgumentException();
        }
        
        return toHexString(md.digest());
    }
    
    public static boolean testPassword(String clearTextTestPassword,
            String encodedActualPassword) {
        clearTextTestPassword = clearTextTestPassword.trim();
        
        String encodedTestPassword = getEncodedPassword(clearTextTestPassword);

        return (encodedTestPassword.equals(encodedActualPassword));
    }
    
    public static String toHexString(byte[] b) {
        StringBuffer hexString = new StringBuffer();
        String plainText;
        for (int i = 0; i < b.length; i++) {
            plainText = Integer.toHexString(0xFF & b[i]);
            if (plainText.length() < 2) {
                plainText = "0" + plainText;
            }
            hexString.append(plainText);
        }
        return hexString.toString();
    }
    
    public static String createId() {
//    	UUID uuid=UUID.randomUUID();
//    	String id="";
//    	String[] ids=uuid.toString().split("-");
//    	for(int i=0;i<ids.length;i++){
//    		id+=ids[i];
//    	}
//		return id;
    	return DateUtils2.strNowtimeSi()+DateUtils2.randomChars(3);
	}
    
    public static int createIntId() {
    	Random rand = new Random();
    	int i = rand.nextInt(); //int范围类的随机数
    	i = rand.nextInt(10000); //生成0-10000以内的随机数

    	i = (int)(Math.random() * 10000); //0-10000以内的随机数，用Matn.random()方式 
		return i;
	}
    
    public static String createUUID() {
    	UUID uuid=UUID.randomUUID();
    	String id="";
    	id = uuid.toString().replaceAll("-", "");
		return id;
	}
    
    public static void main(String args[]){
//    	System.out.println(DateUtils2.strNowtimeS().toString().replaceAll("-", ""));
    	EncryptUtil.createId();
    	System.out.println(EncryptUtil.createUUID());
    	System.out.println(EncryptUtil.getEncodedPassword("admin"));
    	System.out.println(EncryptUtil.getEncodedPassword("123456789"));
    }
}
